package com.qcby.hengxiang.learn.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("login")
public class LoginController {

    @Autowired
    private HttpSession session;


    @RequestMapping("index")
    public String index(Model model){
        //todo 做登录动作
        return "loginform";
    }

    @RequestMapping("login")
    @ResponseBody
    public String login(String username, String password, HttpServletRequest request, HttpServletResponse response){
        //todo 做登录动作
        System.out.println(username + "=" + password);
        Cookie cookie = new Cookie("heaboyid","123qweasdf"+username);
        response.addCookie(cookie);
        response.addCookie(new Cookie("username",username));
        session.setAttribute("heaboyid","123qweasdf"+username);
        return "index";
    }
    @RequestMapping("login1")
    @ResponseBody
    public String login1(String username, String password, HttpServletRequest request, HttpServletResponse response){
        //todo 做登录动作
        Object heaboyid = session.getAttribute("heaboyid");
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println(cookie.getName());
            System.out.println(cookie.getValue());
            if(heaboyid.equals(cookie.getValue())){
                System.out.println("你已经登录过了");
            }
        }


        return "login";
    }
}
